﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using XGameFrame;
using XGameFrame.Graphic;

using XTowerDef.GameScreens;
using XGameFrame.Controls;
using XTowerDef.GameScreens.StartMenuScreenElements;


namespace XTowerDef.GameScreens
{
    public class StartMenuScreen : BaseGameState
    {
        #region Field region
        // a picture for the Background of the startscreen
        Background backgroundImage;
        // a picture for the arrow 
        // with the arrow you can see where you are in the navigation
        PictureBox arrowImage;
        // a link to start the Game
        StartGame startGame;
        // a example Link
        BackToTitle backToTitleScreen;
        // to exit the Game
        ExitGame exitGame;
        // Link to Settings
        StartSettings startSettings;
        // a game describe
        Label advice;
        // Testbutton
        TestButton testButton;
        // Mouse Coord
        MouseCoords mouseCoords;
        //Test Graphic for Animation
        MouseSprite mouseAnimation;
        // the max. width of the LinkLabels
        int maxItemWidth = 50;

        #endregion

        #region Property Region

        #endregion

        #region Constructor Region

        public StartMenuScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            // have to call first so that the ControlManager exists
            base.LoadContent();

            ContentManager Content = Game.Content;
            // the Picture for the Background
            backgroundImage = new Background(
                Content.Load<Texture2D>(@"Backgrounds\titlescreen"),
                GameRef.ScreenRectangle);
            // add the PictureBox to the ControlManager how will add the control to the 
            ControlManager.Add(backgroundImage);

            Texture2D arrowTexture = Content.Load<Texture2D>(@"GUI\leftarrowUp");

            arrowImage = new PictureBox(
                arrowTexture,
                new Rectangle(
                    0,
                    0,
                    arrowTexture.Width,
                    arrowTexture.Height));
            ControlManager.Add(arrowImage);

            startGame = new StartGame(new Vector2(0, 0), "Start xy \na new cool line");
            // that will handle the Selected event of all menu items.
            startGame.Selected += new SelectedControls(menuItem_Selected);

            ControlManager.Add(startGame);

            startSettings = new StartSettings(new Vector2(0, 0), "Settings");
            startSettings.Selected += menuItem_Selected;
            ControlManager.Add(startSettings);

            backToTitleScreen = new BackToTitle(new Vector2(0, 0), "Back to Titlescreen");
            backToTitleScreen.Selected += menuItem_Selected;
            ControlManager.Add(backToTitleScreen);

            advice = new Label(new Vector2(100, 200), "A small advice for the Game");
            ControlManager.Add(advice);

            exitGame = new ExitGame(new Vector2(0, 0), "xy exit");
            exitGame.Selected += menuItem_Selected;
            ControlManager.Add(exitGame);


            Label labelforbutton = new Label(new Vector2(425, 115), "Das ist ein Button");
            ControlManager.Add(labelforbutton);

            Label labelForTime = new CurrentTime(new Vector2(600, 150));
            ControlManager.Add(labelForTime);


            Texture2D testButtonPicnonclick = Content.Load<Texture2D>(@"Controls\Buttons\buttonnonclicked");
            Texture2D testButtonPicHover = Content.Load<Texture2D>(@"Controls\Buttons\buttonhover");
            testButton = new TestButton(new Vector2(450, 150),
                testButtonPicnonclick,
                testButtonPicHover);
            ControlManager.Add(testButton);


            //new testing loading mouse
            Texture2D mouseGraphic = Content.Load<Texture2D>(@"GameGraphics\Mouse\glowing-cursor2");
            mouseAnimation = new MouseSprite(mouseGraphic, InputHandler.CurrentMousePosition);
            StaticSpriteManager.Add(mouseAnimation);

            mouseCoords = new MouseCoords(new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2), "");
            testButton.ClickEvent += new OnClickHandler(testButton_ClickEvent);
            mouseCoords.Visible = false;

            ControlManager.Add(mouseCoords);

            ControlManager.NextControl();

            ControlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);

            Vector2 position = new Vector2(350, 400);


            // In the loop I check to see if the control is a
            // LinkLabel. If it is I compare the X property of its size with maxItemWidth field. If it is greater I set
            // maxItemWidth to that value. I then set the Position property of the control to the Vector2 I created. I
            // then increase the Y property of the Vector2 by the Y property of the Size property of the control plus
            // five pixels.
            foreach (Control c in ControlManager)
            {
                if (c is LinkLabel)
                {
                    if (c.Bounds.Width > maxItemWidth)
                        maxItemWidth = c.Bounds.Width;

                    c.Position = position;
                    position.Y += (int)c.Bounds.Height + 25;

                }
            }

            // I call the FocusChanged event handler passing in the startGame control and
            // null. What this does is call the code that positions the arrow to the right of the sender. In this case, the
            // arrow will be to the right of the startGame item.
            ControlManager_FocusChanged(startGame, null);

           
            
        }

        void testButton_ClickEvent(object o, ClickEventArgs e)
        {
            mouseCoords.ChangeVisibility();
        }
        /// <summary>
        /// accept the sender as a control and place the arrow after the control position
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2((int)control.Position.X + maxItemWidth + 25,
                control.Position.Y);
            arrowImage.SetPosition(position);
        }

        private void menuItem_Selected(object sender, EventArgs e)
        {
            if (sender == startGame)
            {
                StateManager.PushState(GameRef.GamePlayScreen);
            }
            if (sender == startSettings)
            {
                StateManager.PushState(GameRef.SettingsScreen);
            }
            if (sender == backToTitleScreen)
            {
                StateManager.PopState();
            }
            if (sender == exitGame)
            {
                GameRef.Exit();
            }
            
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, playerIndexInControl);
            AnimatedSpriteManager.Update(gameTime, playerIndexInControl);
            StaticSpriteManager.Update(gameTime, playerIndexInControl);
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();
            base.Draw(gameTime);
            ControlManager.Draw(GameRef.SpriteBatch);
            AnimatedSpriteManager.Draw(GameRef.SpriteBatch);
            StaticSpriteManager.Draw(GameRef.SpriteBatch);
            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Game State Method Region

        #endregion

    }
}